home *** CD-ROM | disk | FTP | other *** search
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>3.6.  Extending The Text Box Script</title>
- <link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
- <link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
- <link rel="stylesheet" href="gimp-help-custom.css" type="text/css" />
- <link rel="alternate stylesheet" href="gimp22.css" type="text/css" title="gimp22" />
- <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
- <link rel="start" href="index.html" title="GNU Image Manipulation Program" />
- <link rel="up" href="gimp-using-script-fu-tutorial.html" title="3.  A Script-Fu Tutorial" />
- <link rel="prev" href="gimp-using-script-fu-tutorial-script.html" title="3.5.  Giving Our Script Some Guts" />
- <link rel="next" href="gimp-using-script-fu-tutorial-result.html" title="3.7.  Your script and its working" />
- </head>
- <body>
- <div class="navheader">
- <table width="100%" summary="Navigation header">
- <tr>
- <th colspan="3" align="center">3.6. 
- <span lang="en" xml:lang="en">Extending The Text Box Script</span>
- </th>
- </tr>
- <tr>
- <td width="20%" align="left"><a accesskey="p" href="gimp-using-script-fu-tutorial-script.html"><img src="../images/prev.png" alt="Prev" /></a> </td>
- <th width="60%" align="center">3. 
- <span lang="en" xml:lang="en">A Script-Fu Tutorial</span>
- </th>
- <td width="20%" align="right"> <a accesskey="n" href="gimp-using-script-fu-tutorial-result.html"><img src="../images/next.png" alt="Next" /></a></td>
- </tr>
- </table>
- <hr />
- </div>
- <div class="sect2" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h3 class="title"><a id="gimp-using-script-fu-tutorial-extending-text-box"></a>3.6. 
- <span lang="en" xml:lang="en">Extending The Text Box Script</span>
- </h3>
- </div>
- </div>
- </div>
- <div class="sect3" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id2619882"></a>3.6.1. 
- <span lang="en" xml:lang="en">Handling Undo Correctly</span>
- </h4>
- </div>
- </div>
- </div>
- <p>
- When creating a script, you want to give your users the ability to
- undo their actions, should they make a mistake. This is easily
- accomplished by calling the functions
- <code class="code">gimp-undo-push-group-start</code>
- and <code class="code">gimp-undo-push-group-end</code>
- around the code that manipulates the image. You can think of them as
- matched statements that let GIMP know when to start and stop recording
- manipulations on the image, so that those manipulations can later be
- undone.
- </p>
- <p>
- If you are creating a new image entirely, it doesn't make sense to use
- these functions because you're not changing an existing image.
- However, when you are changing an existing image, you most surely want
- to use these functions.
- </p>
- <p>
- Undoing a script works nearly flawlessly when using these functions.
- </p>
- </div>
- <div class="sect3" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id2619932"></a>3.6.2. 
- <span lang="en" xml:lang="en">Extending The Script A Little More</span>
- </h4>
- </div>
- </div>
- </div>
- <p>
- Now that we have a very handy-dandy script to create text
- boxes, let's add two features to it:
- </p>
- <div class="itemizedlist">
- <ul type="disc">
- <li>
- <p>
- Currently, the image is resized to fit exactly around the
- text -- there's no room for anything, like drop shadows or
- special effects (even though many scripts will automatically
- resize the image as necessary). Let's add a buffer around
- the text, and even let the user specify how much buffer to
- add as a percentage of the size of the resultant text.
- </p>
- </li>
- <li>
- <p>
- This script could easily be used in other scripts that work
- with text. Let's extend it so that it returns the image and
- the layers, so other scripts can call this script and use
- the image and layers we create.
- </p>
- </li>
- </ul>
- </div>
- </div>
- <div class="sect3" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id2619983"></a>3.6.3. 
- <span lang="en" xml:lang="en">
- Modifying The Parameters And The Registration Function
- </span>
- </h4>
- </div>
- </div>
- </div>
- <p>
- To let the user specify the amount of buffer, we'll add a parameter to
- our function and the registration function:
- </p>
- <pre class="programlisting">
- (define (script-fu-text-box inTest inFont inFontSize inTextColor inBufferAmount)
- (let*
- (
- ; define our local variables
- ; create a new image:
- (theImageWidth 10)
- (theImageHeight 10)
- (theImage (car
- (gimp-image-new
- theImageWidth
- theImageHeight
- RGB
- )
- )
- )
- (theText) ;a declaration for the text
- ;we create later
- (theBuffer) ;<span class="emphasis"><em>added</em></span>
- (theLayer
- (car
- (gimp-layer-new
- theImage
- theImageWidth
- theImageHeight
- RGB-IMAGE
- "layer 1"
- 100
- NORMAL
- )
- )
- )
- ) ;end of our local variables
- <em class="replaceable"><code>[Code here]</code></em>
- )
- </pre>
- <pre class="programlisting">
- (script-fu-register
- "script-fu-text-box" ;func name
- "Text Box" ;menu label
- "Creates a simple text box, sized to fit\
- around the user's choice of text,\
- font, font size, and color." ;description
- "Michael Terry" ;author
- "copyright 1997, Michael Terry" ;copyright notice
- "October 27, 1997" ;date created
- "" ;image type that the script works on
- SF-STRING "Text:" "Text Box" ;a string variable
- SF-FONT "Font:" "Charter" ;a font variable
- SF-ADJUSTMENT "Font size" '(50 1 1000 1 10 0 1)
- ;a spin-button
- SF-COLOR "Color:" '(0 0 0) ;color variable
- SF-ADJUSTMENT "Buffer amount" '(35 0 100 1 10 1 0)
- ;a slider
- )
- (script-fu-menu-register "script-fu-text-box" "<Toolbox>/Xtns/Script-Fu/Text")
- </pre>
- </div>
- <div class="sect3" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id2620082"></a>3.6.4. 
- <span lang="en" xml:lang="en">Adding The New Code</span>
- </h4>
- </div>
- </div>
- </div>
- <p>
- We're going to add code in two places: right before we resize
- the image, and at the end of the script (to return the new
- image, the layer and the text).
- </p>
- <p>
- After we get the text's height and width, we need to resize
- these values based on the buffer amount specified by the
- user. We won't do any error checking to make sure it's in the
- range of 0-100% because it's not life-threatening, and because
- there's no reason why the user can't enter a value like "200"
- as the percent of buffer to add.
- </p>
- <pre class="programlisting">
- (set! theBuffer (* theImageHeight (/ inBufferAmount 100) ) )
- (set! theImageHeight (+ theImageHeight theBuffer theBuffer) )
- (set! theImageWidth (+ theImageWidth theBuffer theBuffer) )
- </pre>
- <p>
- All we're doing here is setting the buffer based on the height of the
- text, and adding it twice to both the height and width of our new
- image. (We add it twice to both dimensions because the buffer needs to
- be added to both sides of the text.)
- </p>
- <p>
- Now that we have resized the image to allow for a buffer, we
- need to center the text within the image. This is done by
- moving it to the (x, y) coordinates of (<code class="varname">theBuffer</code>,
- <code class="varname">theBuffer</code>). I added this line after
- resizing the layer and the image:
- </p>
- <pre class="programlisting">
- (gimp-layer-set-offsets theText theBuffer theBuffer)
- </pre>
- <p>
- Go ahead and save your script, and try it out after refreshing the
- database.
- </p>
- <p>
- All that is left to do is return our image, the layer, and the text
- layer. After displaying the image, we add this line:
- </p>
- <pre class="programlisting">
- (list theImage theLayer theText)
- </pre>
- <p>
- This is the last line of the function, making this list available to
- other scripts that want to use it.
- </p>
- <p>
- To use our new text box script in another script, we could write
- something like the following:
- </p>
- <pre class="programlisting">
- (set! theResult (script-fu-text-box
- "Some text"
- "Charter" "30"
- '(0 0 0)
- "35"
- )
- )
- (gimp-image-flatten (car theResult))
- </pre>
- <p>
- Congratulations, you are on your way to your Black Belt of Script-Fu!
- </p>
- </div>
- </div>
- <div class="navfooter">
- <hr />
- <table width="100%" summary="Navigation footer">
- <tr>
- <td width="40%" align="left"><a accesskey="p" href="gimp-using-script-fu-tutorial-script.html"><img src="../images/prev.png" alt="Prev" /></a> </td>
- <td width="20%" align="center">
- <a accesskey="u" href="gimp-using-script-fu-tutorial.html">
- <img src="../images/up.png" alt="Up" />
- </a>
- </td>
- <td width="40%" align="right"> <a accesskey="n" href="gimp-using-script-fu-tutorial-result.html"><img src="../images/next.png" alt="Next" /></a></td>
- </tr>
- <tr>
- <td width="40%" align="left" valign="top"><a accesskey="p" href="gimp-using-script-fu-tutorial-script.html">3.5. 
- <span lang="en" xml:lang="en">Giving Our Script Some Guts</span>
- </a> </td>
- <td width="20%" align="center">
- <a accesskey="h" href="index.html">
- <img src="../images/home.png" alt="Home" />
- </a>
- </td>
- <td width="40%" align="right" valign="top"> <a accesskey="n" href="gimp-using-script-fu-tutorial-result.html">3.7. 
- <span lang="en" xml:lang="en">Your script and its working</span>
- </a></td>
- </tr>
- </table>
- </div>
- </body>
- </html>
-